package com.my.springbootsecurityjwt.config.security;

import cn.hutool.json.JSONUtil;
import com.my.common.result.R;
import com.my.common.result.ServletUtils;
import jakarta.servlet.ServletException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import java.io.IOException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
/**
 *  认证失败处理类
 *  当未登录或者token失效访问接口时，自定义的返回结果
 *
 * @author: marshal
 * @date: 2021-10-15 3:20 PM
 */
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        ServletUtils.renderString(response, JSONUtil.toJsonStr(R.unauthorized(authException.getMessage())));
    }
}
